function  [bdraw,Qb,m1,CC1]=drawncd(y,x,B0,A0)
%function [bdraw,Qb,m1,CC1]=drawncd(y,x,B0,A0); 
% Draw Coefficients from a Normal distribution when prior is also 
%      prior(B|sigma) ~ Normal( B0, sigma*A0^(-1) ) 
%      i.e. prior is conditional on sigma 
%      A0 the precision
% 
%      Formulas foollow Chib JE (1993) 
%      When beta and sigma are assumed independent a priori  
%      use drawnorm.m 
%
%      Use in conjunction with drawsigcd.m 
%      
% y     (Txn) 
% x     (TxK)  
% B0    (Kx1)  Prior Mean 
% AO    (KxK)  Prior Precision
%
% bdraw  (1xK)  Draw from the posterior normal as a row vector
% Qb     (1x1)  (bdraw-B0)'*A0*(bdraw-BO) needed for 
%               the posterior of sigma 
%
% m1     (1xK)  Posterior mean 
% CC1    (KxK)  Cholelsky of the posterior variance 
%               V1= CC1'*CC1  
[ty,ny]=size(y); 
[tx,nx]=size(x); 
nk=length(B0);
if tx~=ty
    error('Not equal number of observations in drawcoef'); 
end; 
V1=( A0 + (x'*x)  ) \ eye(nk) ; % Posterior Variance  
m1=V1*(A0*B0  + x'*y );        % Posterior Mean  
CC1=chol(V1); 
bdraw=m1'+ randn(1,nk)*CC1;
m1=m1'; 
Qb=( (bdraw'-B0)' )*A0*(bdraw'-B0); 
